package br.usp.pcs.basys2.event;

import java.util.Calendar;

import br.usp.pcs.basys2.BasysProcess;
import br.usp.pcs.basys2.info.JobInfo;

public abstract class Event implements Comparable<Event> {
	private BasysProcess process;
	private final Calendar dateOfArrival;
	private JobInfo job;
	
	public EventInfo getInfo(){
		EventInfo eventInfo = new EventInfo();
		eventInfo.setDateOfArrival(this.dateOfArrival.getTimeInMillis());
		eventInfo.setName(this.getClass().getSimpleName());
		eventInfo.setProcessId(this.process == null? null:this.process.getId().toString());
		return eventInfo;
	}

	public BasysProcess getProcess() {
		return process;
	}

	public Calendar getDateOfArrival() {
		return dateOfArrival;
	}
	
	public Event(BasysProcess process, Calendar dateOfArrival) {
		super();
		if (process == null || dateOfArrival == null){
			throw new IllegalArgumentException();
		}
		this.process = process;
		this.dateOfArrival = dateOfArrival;
	}

	public Event(JobInfo job, Calendar dateOfArrival) {
		super();
		if (job == null || dateOfArrival == null){
			throw new IllegalArgumentException();
		}
		this.job = job;
		this.dateOfArrival = dateOfArrival;
	}

	public abstract void execute();

	public int compareTo(Event e) {
		return this.dateOfArrival.compareTo(e.dateOfArrival);
	}

	@Override
	public String toString() {
		return String.format("%05d", dateOfArrival.getTimeInMillis())  + " " + this.getClass().getSimpleName() + " id:" + process;
	}

	public JobInfo getJob() {
		return job;
	}
}
